package com.study.stream;

import com.alibaba.fastjson.JSON;
import com.study.vo.StudyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 使用默认的通信信道实现消息的发送
 */
@Slf4j
@EnableBinding(Source.class)
public class DefaultSendService {

    @Autowired
    private Source source;

    public void sendMessage(StudyMessage message){

        String _message = JSON.toJSONString(message);
        log.info("DefaultSendService send message:[{}]",
                _message);

        //spring Message 统一消息的编程模型、是stream组件的重要组成部分之一
        source.output().send(MessageBuilder.withPayload(_message).build());
    }
}
